package com.myhome.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myhome.config.constant.R;
import com.myhome.domain.Notice;
import com.myhome.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @Author QYiXin
 * @Date 2021 10 12 16 55
 **/
@RestController
@Api(tags = "notice控制器")
@RequestMapping("/notices")
public class NoticeController {
    @Autowired
    NoticeService noticeService;

    @GetMapping
    @ApiOperation(value = "分页查询公告")
    @PreAuthorize("hasAuthority('notice_query')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数"),
            @ApiImplicitParam(name = "title", value = "公告的标题"),
            @ApiImplicitParam(name = "startTime", value = "公告的创建开始时间"),
            @ApiImplicitParam(name = "endTime", value = "公告的创建结束时间时间"),
    })
    public R<Page<Notice>> getPagesNotices(Page<Notice> page, String title, String startTime, String endTime) {
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<Notice> result = noticeService.getPagesNotices(page, title, startTime, endTime);
        return R.ok(result);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除一个公告")
    @PreAuthorize("hasAuthority('notice_delete')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "要删除的id的集合")
    })
    public R deleteNotice(String[] ids) {
        boolean isDelete = noticeService.removeByIds(Arrays.asList(ids));
        if (isDelete) {
            return R.ok();
        }
        return R.fail("删除失败");
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "启用/禁用一个公告")
    @PreAuthorize("hasAuthority('notice_update')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要启用/禁用一个公告的公告Id"),
            @ApiImplicitParam(name = "status", value = "要设置的公告状态")

    })
    public R updateStatus(Long id, Integer status) {
        Notice notice = new Notice();
        notice.setStatus(status);
        notice.setId(id);
        boolean isUpdate = noticeService.updateById(notice);
        if (isUpdate) {
            return R.ok();
        }
        return R.fail("修改失败");
    }

    @PostMapping
    @ApiOperation(value = "新增一个公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "notice", value = "notice的json数据")
    })
    public R addNotice(@RequestBody Notice notice) {
        notice.setStatus(1);
        boolean isSave = noticeService.save(notice);
        if (isSave) {
            return R.ok();
        }
        return R.fail("新增失败");
    }

    @PatchMapping
    @ApiOperation("修改公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "notice", value = "修改数据")
    })
    public R updateNotice(@RequestBody Notice notice) {
        boolean isUpdate = noticeService.updateById(notice);
        if (isUpdate) {
            return R.ok();
        }
        return R.fail("修改失败");
    }

    @GetMapping("/simple")
    @ApiOperation("获取简单工单")
    public R<Page<Notice>> getSimpleNotice(Page<Notice> page) {
        Page<Notice> result = noticeService.getAllSimpleNotice(page);
        return R.ok(result);
    }

    @GetMapping("/simple/{id}")
    @ApiOperation("获取单个通知详情")
    public R<Notice> getOneNoticeSimpleInfo(@PathVariable("id") Long id) {
        return R.ok(noticeService.getById(id));
    }
}
